package com.itstorm.pomeloweather.ui;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

import com.itstorm.pomeloweather.R;
import com.itstorm.pomeloweather.util.BroadcastCode;

public class EditAreasActivity extends Activity {

	private ImageButton imgbtnBack;
	private ListView listview;
	private AreasListAdapter areasListAdapter;
	private SharedPreferences weatherList;
	private SharedPreferences.Editor editor;
	private String areas;
	private String[] areasArray;
	private Display mDisplay;
	private BroadcastReceiver mReceiver;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit_areas);
		initialization();

	}

	private void initialization() {
		mDisplay = EditAreasActivity.this.getWindowManager()
				.getDefaultDisplay();
		weatherList = getSharedPreferences("WeatherList", 0);
		editor = weatherList.edit();
		areas = weatherList.getString("Areas", "");
		areasArray = areas.split(",");
		areasListAdapter = new AreasListAdapter(EditAreasActivity.this,
				areasArray, mDisplay);
		listview = (ListView) findViewById(R.id.listView);
		listview.setAdapter(areasListAdapter);

		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					final int position, long id) {
				// TODO Auto-generated method stub

			}
		});

		imgbtnBack = (ImageButton) findViewById(R.id.imgbtn_back);

		imgbtnBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				overridePendingTransition(R.anim.fade, R.anim.fade);
				EditAreasActivity.this.finish();
			}
		});

		mReceiver = new BroadcastReceiver() {
			@Override
			public void onReceive(Context context, Intent intent) {
				// TODO Auto-generated method stub
				if (intent.getAction().equals(BroadcastCode.changeAreas)) {
					Bundle extras = intent.getExtras();
					int type = extras.getInt("type", 0);
					switch (type) {
					case BroadcastCode.delArea:
						// int index = extras.getInt("index", 0);

						weatherList = context.getSharedPreferences(
								"WeatherList", 0);
						String[] mAreasArray = weatherList.getString("Areas", "").split(
								",");
						Log.d("JSON",
								"---------------0 new city---------------\n"
										+ weatherList.getString("Areas", "").toString());
						areasListAdapter.changeList(mAreasArray);
						areasListAdapter.notifyDataSetChanged();
//						listview.setAdapter(areasListAdapter);
						break;
					default:
						break;
					}
				}
			}
		};
		IntentFilter mIntentFilter = new IntentFilter(BroadcastCode.changeAreas);
		registerReceiver(mReceiver, mIntentFilter);

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(mReceiver);
	}
}
